package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.PracticeCadetAttendance;
import com.ruoyi.system.domain.PracticeCadetAttendanceReport;
import com.ruoyi.system.service.IPracticeCadetAttendanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实践考勤Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"实践考勤"})
@RestController
@RequestMapping("/practice/attendance")
public class PracticeCadetAttendanceController extends BaseController {
    @Autowired
    private IPracticeCadetAttendanceService practiceCadetAttendanceService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询实践考勤列表
     */
    @ApiOperation(value = "实践考勤列表", notes = "查询实践考勤列表详情", tags = {"实践考勤管理"})
    //@PreAuthorize("@ss.hasPermi('practice:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(PracticeCadetAttendance practiceCadetAttendance) {
        startPage();
        List<PracticeCadetAttendance> list = practiceCadetAttendanceService.selectPracticeCadetAttendanceList(practiceCadetAttendance);
        return getDataTable(list);
    }

    /**
     * 查询实践考勤报表
     */
    @ApiOperation(value = "实践考勤报表", notes = "查询实践考勤列表详情", tags = {"实践考勤管理"})
    //@PreAuthorize("@ss.hasPermi('practice:attendance:list')")
    @GetMapping("/listReport")
    public TableDataInfo listReport(PracticeCadetAttendanceReport practiceCadetAttendance) {
        startPage();
        List<PracticeCadetAttendanceReport> list = practiceCadetAttendanceService.selectPracticeCadetAttendanceReport(practiceCadetAttendance);
        return getDataTable(list);
    }

    /**
     * 导出实践考勤列表
     */
    @ApiOperation(value = "实践考勤列表导出", notes = "导出实践考勤列表", tags = {"实践考勤管理"})
    //@PreAuthorize("@ss.hasPermi('practice:attendance:export')")
    @Log(title = "实践考勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PracticeCadetAttendance practiceCadetAttendance) {
        List<PracticeCadetAttendance> list = practiceCadetAttendanceService.selectPracticeCadetAttendanceList(practiceCadetAttendance);
        ExcelUtil<PracticeCadetAttendance> util = new ExcelUtil<PracticeCadetAttendance>(PracticeCadetAttendance.class);
        util.exportExcel(response, list, "实践考勤数据");
    }

    /**
     * 获取实践考勤详细信息
     *
     * @ApiOperation(value = "实践考勤详细信息", notes = "获取实践考勤详细信息详情", tags = {"实践考勤管理"})
     * //@PreAuthorize("@ss.hasPermi('practice:attendance:query')")
     * @GetMapping(value = "/{attendanceId}")
     * public AjaxResult getInfo(@PathVariable("attendanceId") Long attendanceId) {
     * return success(practiceCadetAttendanceService.selectPracticeCadetAttendanceByAttendanceId(attendanceId));
     * }
     * <p>
     * /**
     * 新增实践考勤
     */
    @ApiOperation(value = "新增实践考勤", notes = "新增实践考勤详情", tags = {"实践考勤管理"})
    //@PreAuthorize("@ss.hasPermi('practice:attendance:add')")
    @Log(title = "实践考勤", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody PracticeCadetAttendance practiceCadetAttendance) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        practiceCadetAttendanceService.completeData(practiceCadetAttendance, userId, 1);
        return toAjax(practiceCadetAttendanceService.insertPracticeCadetAttendance(practiceCadetAttendance));
    }

    /**
     * 修改实践考勤
     */
    @ApiOperation(value = "修改实践考勤", notes = "修改实践考勤详情", tags = {"实践考勤管理"})
    //@PreAuthorize("@ss.hasPermi('practice:attendance:edit')")
    @Log(title = "实践考勤", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody PracticeCadetAttendance practiceCadetAttendance) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        practiceCadetAttendanceService.completeData(practiceCadetAttendance, userId, 2);
        return toAjax(practiceCadetAttendanceService.updatePracticeCadetAttendance(practiceCadetAttendance));
    }

    /**
     * 删除实践考勤
     */
    @ApiOperation(value = "删除实践考勤", notes = "删除实践考勤详情", tags = {"实践考勤管理"})
    //@PreAuthorize("@ss.hasPermi('practice:attendance:remove')")
    @Log(title = "实践考勤", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attendanceIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceIds) {
        return toAjax(practiceCadetAttendanceService.deletePracticeCadetAttendanceByAttendanceIds(attendanceIds));
    }
}
